package ltd.jdsoft.cute.prometheus.spi.impl;

import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.prometheus.client.Counter;
import ltd.jdsoft.cute.prometheus.MonitorChain;
import ltd.jdsoft.cute.prometheus.spi.Monitor;

/**
 * 接口调用异常次数计数器
 * 
 * @author zhengzhq
 *
 */
public class ExceptionCounter implements Monitor {

  private static Logger logger = LoggerFactory.getLogger(ExceptionCounter.class);
  private static Counter exceptionCounter =
      Counter.build().name("exception_counter").help("接口失败次数").labelNames(LABEL_NAMES).register();

  @Override
  public Object process(ProceedingJoinPoint joinPoint, MonitorChain chain, String... labelValues)
      throws Throwable {
    logger.debug("start monitor exception count...");
    try {
      return chain.doChain(joinPoint, chain);
    } catch (Throwable throwable) {
      exceptionCounter.inc();
      throw throwable;
    } finally {
      logger.debug("end monitor exception count...");
    }
  }
}
